package com.sinochem.yunlian.ship.base.rest.manage;

import java.util.List;

import com.sinochem.yunlian.ship.base.biz.BsPortBiz;
import com.sinochem.yunlian.ship.common.annotation.IgnoreToken;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sinochem.yunlian.ship.base.entity.BsPort;
import com.sinochem.yunlian.ship.base.mapper.BsPortMapper;
import com.sinochem.yunlian.ship.common.msg.RestResponse;

/**
 * 初始化港口数据表编码
 */
@IgnoreToken
@RestController
@RequestMapping("/anon/test")
public class Test {

	@Autowired
	private BsPortBiz bsPortBiz;

	@Autowired
	private BsPortMapper portMapper;
	
	private Logger log = LoggerFactory.getLogger(Test.class);
	
	@RequestMapping(value = "/enName",method = RequestMethod.GET)
	public RestResponse<String> setEnName(Integer level) {
		
		List<BsPort> ports = bsPortBiz.selectPortByLeve(level);
		
		if (ports == null || ports.isEmpty()) {
			return RestResponse.success("SUCCESS");
		}
		
		for (BsPort port : ports) {
			 // 获取当前节点最大编码
			String maxEnName = portMapper.selectPortMaxEnNameNotNull(port.getParentId());
			
            boolean isOneLine = false;
            if (maxEnName == null) {
                maxEnName = portMapper.selectPortMaxEnNameById(port.getId());
                isOneLine = true;
            }  
            if (maxEnName == null) {
            	maxEnName = portMapper.selectPortMaxEnNameById(port.getParentId());
                isOneLine = true;
            }
            String enName = getEnName(maxEnName, 1, isOneLine, port.getLevel());
            log.info("输出enName:{}", enName);
            portMapper.updatePortEnNameById(port.getId(), enName);
		}
		
		return RestResponse.success("SUCCESS");
		
	}
	
	public static String getEnName(String enName, Integer increase, boolean isOneLine, Integer level) {
		

		if (isOneLine && level != null && 4 == level) {
			// 四类货品
			if (StringUtils.isNotBlank(enName)) {
				
				return enName + "00";
			} else {
				return "00";
			}
			
			
		} else if (isOneLine) {
			// 一、二、三类货品
			if (StringUtils.isNotBlank(enName)) {
				return enName + "01";
			} else {
				return "01";
			}
		}
		// 编码长度
		int strLen = enName.length();
		// 转为Integer类型
		Integer value = Integer.parseInt(enName);
		// 前缀是否存在0
		int residuLen = strLen - ((Integer)Integer.parseInt(enName)).toString().length();
		// 当前值加上某个值，得到最新值
		Integer newValue = value + increase;
		StringBuffer sBuffer =  new StringBuffer();
		if (residuLen > 0 && strLen > newValue.toString().length()) {
			// 存在前缀0
			for (int i = 0 ; i < residuLen; i++) {
				sBuffer.append(0);
			}
		} 
		sBuffer.append(newValue);
		return sBuffer.toString();
	}

}
